<?php

namespace app\index\service;

use think\Db;

/**
 * 分销业务类
 * Class Distribution
 * @package app\index\service
 */
class Distribution
{
    protected static $is_vip = [
        0 => '普通会员',
        1 => 'VIP会员',
        2 => '超级VIP会员',
        3 => '金钻VIP会员',
    ];

    /**
     * 获取当前用户下面的三级用户
     * @param int $user_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOfflineThirdLevelUsers(int $user_id): array
    {
        $users = [
            'one' => [],
            'two' => [],
            'three' => [],
        ];

        $recommends = DB::table('zh_recommend')->where("parent like '%|{$user_id}|%' and member_id <> {$user_id}")->select();

        if (!empty($recommends)) {
            foreach ($recommends as $value) {
                $parent = explode('_', $value['parent']);
                $count = count($parent);

                if (!$count || $count == 1) {
                    continue;
                }

                foreach ($parent as &$item) {
                    $item = trim($item, '|');
                }

                // 取得当前值在数组中的位置
                $key = array_search($user_id, $parent);

                // 获取一级用户
                $one = $parent[$key + 1] ?? 0;
                if ($one > 0 && !in_array($one, $users['one'])) {
                    $users['one'][] = $one;
                }

                // 获取二级用户
                $two = $parent[$key + 2] ?? 0;
                if ($two > 0 && !in_array($two, $users['two'])) {
                    $users['two'][] = $two;
                }

                // 获取三级用户
                $three = $parent[$key + 3] ?? 0;
                if ($three > 0 && !in_array($three, $users['three'])) {
                    $users['three'][] = $three;
                }
            }
        }

        return $users;
    }

    /**
     * 获取三级内的下级人员
     * @param int $user_id
     * @param int $level
     * @param int $page
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getSubordinatePersonnel(int $user_id, int $level, int $page, int $limit): array
    {
        $users = [];

        $recommends = DB::table('zh_recommend')
            ->where("parent like '%|{$user_id}|%' and member_id <> {$user_id}")
            ->limit($page, $limit)
            ->select();

        if (!empty($recommends)) {
            foreach ($recommends as $value) {
                $parent = explode('_', $value['parent']);
                $count = count($parent);

                if (!$count || $count == 1) {
                    continue;
                }

                foreach ($parent as &$item) {
                    $item = trim($item, '|');
                }

                // 取得当前值在数组中的位置
                $key = array_search($user_id, $parent);

                // 获取一级用户
                if ($level == 1) {
                    $one = $parent[$key + 1] ?? 0;
                    if ($one > 0 && !in_array($one, $users)) {
                        $users[] = $one;
                    }
                }

                // 获取二级用户
                if ($level == 2) {
                    $two = $parent[$key + 2] ?? 0;
                    if ($two > 0 && !in_array($two, $users)) {
                        $users[] = $two;
                    }
                }

                // 获取三级用户
                if ($level == 3) {
                    $three = $parent[$key + 3] ?? 0;
                    if ($three > 0 && !in_array($three, $users)) {
                        $users[] = $three;
                    }
                }
            }
        }

        return $users;
    }

    /**
     * 根据用户id获取用户信息
     * @param array $user_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserInfo(array $user_id): array
    {
        $users = [];
        $field = "avatar,username,phone,is_vip as level,FROM_UNIXTIME(create_time,'%Y.%m.%d') as create_time";

        if (is_array($user_id) && count($user_id)) {
            foreach ($user_id as $item) {
                $where = "member_id  = {$item}";
                $user = DB::table('zh_members')->field($field)->where($where)->find();

                if (!empty($user)) {
                    $user['level'] = self::$is_vip[$user['level']];
                    $users[] = $user;
                }
            }
        }

        return $users;
    }
}